package com.bigfat.baidumaptest; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.model.LatLng; import java.util.List; public class MainActivity extends ActionBarActivity { public static final String TAG = "MainActivity"; private MapView mapView; private BaiduMap baiduMap; private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化百度地图 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.map_view); baiduMap = mapView.getMap(); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); List<String> providerList = locationManager.getProviders(true); if (providerList.contains(LocationManager.GPS_PROVIDER)) { provider = LocationManager.GPS_PROVIDER; } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) { provider = LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show(); return; } Location location = locationManager.getLastKnownLocation(provider); if (location != null) { navigateTo(location); } locationManager.requestLocationUpdates(provider, 3000, 0, new LocationListener() { @Override public void onLocationChanged(Location location) { navigateTo(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }); } private void navigateTo(Location location) { if (location != null) { Log.d(TAG, "Latitude:" + location.getLatitude() + "Longitude:" + location.getLongitude()); LatLng p = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus mapStatus = new MapStatus.Builder(baiduMap.getMapStatus()).zoom(14).target(p).build(); baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mapStatus)); } } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }